package com.aimee.servlet;

import com.aimee.dao.ExamPaperDAO;
import com.aimee.dao.ExamQuestionDAO;
import com.aimee.dao.StuExamDAO;
import com.aimee.entity.ExamPaper;
import com.aimee.entity.ExamPaperQuestion;
import com.aimee.entity.ExamRecord;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@WebServlet("/admin/stuexam")
public class StuExamServlet extends BaseServlet {
    StuExamDAO dao = new StuExamDAO();
    ExamQuestionDAO eqdao = new ExamQuestionDAO();

    //我的考试
    protected void showmyexam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();

        int method = Integer.parseInt(request.getParameter("method"));

        //查出当前登录的用户id
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println(user_id);

        List myexamlist = null;
        int count = 0;
        if (method == 1) {
            System.out.println("显示所有进行中的考试");
            myexamlist = dao.showmyexam(user_id, "进行中");
            count = (int) dao.countmyallexam(user_id, "进行中");
//            request.setAttribute("method", method);
        } else {
            System.out.println("按照条件查找考试");
//            int course = Integer.parseInt(request.getParameter("course"));
            String beginTime = request.getParameter("beginTime");
            String endTime = request.getParameter("endTime");
            System.out.println(beginTime + "--" + endTime);
            myexamlist = dao.findmyexambysth(user_id, beginTime, endTime);
            count = (int) dao.countmyexambysth(user_id, beginTime, endTime);
//            request.setAttribute("method", method);
        }
        System.out.println(myexamlist);
        String list = gson.toJson(myexamlist);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\":" + count + ",\n" +
                "  \"data\":" + list + "\n" +
                "} ";
        System.out.println(res);
        out.print(res);
        out.flush();
        out.close();


    }

    //我的考试记录
    protected void showmyexamrecord(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();

        int method = Integer.parseInt(request.getParameter("method"));

        //查出当前登录的用户id
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println(user_id);

//        List<ExamRecord> examstate=dao.findmyexamstate();
        List myexamlist = null;
        int count = 0;
        if (method == 1) {
            System.out.println("显示所有进行中的考试");
            myexamlist = dao.showmyexamrecord(100003);
            count = Math.toIntExact(dao.countallmyexamrecord(100003));
        } else {
            System.out.println("按照条件查找考试");
            int course = Integer.parseInt(request.getParameter("course"));
            myexamlist = dao.findmyexamrecordbysth(user_id, course);
            count = Math.toIntExact(dao.countmyexamrecordbysth(user_id, course));
//            request.setAttribute("myexamlist", myexamlist);
        }
        System.out.println(myexamlist);
        String list = gson.toJson(myexamlist);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\":" + count + ",\n" +
                "  \"data\":" + list + "\n" +
                "} ";
        System.out.println(res);
        out.print(res);
        out.flush();
        out.close();

//        request.getRequestDispatcher("/user/myexam.jsp").forward(request, response);
    }

    /*
    开始考试
    */
    //显示试卷内容
    public void findmyexamrecordbysth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("开始考试试卷内容");
        //查出当前登录的用户id
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println(user_id);
        int exam_id = Integer.parseInt(request.getParameter("exam_id"));
        int examrecord_id = Integer.parseInt(request.getParameter("examrecord_id"));
        System.out.println(examrecord_id);
        String exam_end_date = request.getParameter("exam_end_date");
//        exam_end_date = new String(exam_end_date.getBytes("ISO-8859-1"), "utf-8");
        String exam_start_date = request.getParameter("exam_start_date");
//        exam_start_date = new String(exam_start_date.getBytes("ISO-8859-1"), "utf-8");
        List examquestionlist = null;
        examquestionlist = eqdao.findexamquestionsbyid(exam_id);
//        int count = Math.toIntExact(eqdao.countexamquestionbyid(exam_id));

        System.out.println(examquestionlist);
        System.out.println(exam_start_date + "-" + exam_end_date);

        request.setAttribute("user_id", user_id);
        request.setAttribute("examrecord_id", examrecord_id);
//        request.setAttribute("examquestioncount", count);
        request.setAttribute("examquestionlist", examquestionlist);
        request.setAttribute("exam_end_date", exam_end_date);
        request.setAttribute("exam_start_date", exam_start_date);
        request.getRequestDispatcher("/user/examing.jsp").forward(request, response);

    }

    //超时未考试修改考试状态
    public void updateexamstateifnoexam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //查出当前登录的用户id
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println(user_id);
        int exam_id = Integer.parseInt(request.getParameter("exam_id"));
        int n = dao.updateexamstateifnoexam(user_id, exam_id);
        System.out.println(n);
        response.sendRedirect("/OnlineExam/user/examing.jsp");
    }

    //提交考试修改考试状态
    public void updateexamstateifsubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //查出当前登录的用户id
  /*      HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println(user_id);*/
        int exam_score = Integer.parseInt(request.getParameter("exam_score"));
        int exam_usetime = Integer.parseInt(request.getParameter("exam_usetime"));
        int examrecord_id = Integer.parseInt(request.getParameter("examrecord_id"));
        System.out.println("修改考试分数" + exam_score + "时间" + exam_usetime + "记录" + examrecord_id);
        int n = dao.updatescoreetc(exam_score, exam_usetime, examrecord_id);
        System.out.println(n);
        response.sendRedirect("/OnlineExam/user/myexam.jsp");
    }

    //查看答卷内容
    public void checkexampaperquestion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
       System.out.println("查看考试答案");
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println(user_id);
        int examrecord_id = Integer.parseInt(request.getParameter("examrecord_id"));
        int exampaper_id = Integer.parseInt(request.getParameter("exampaper_id"));
        int exam_id = Integer.parseInt(request.getParameter("exam_id"));
        System.out.println("考试id" + exam_id);
        System.out.println("考试记录id" + examrecord_id);
        System.out.println("试卷id" + exampaper_id);

        List examquestionlist = null;
        examquestionlist= eqdao.findexamquestionsbyid(exam_id);
        List examrecordlist = null;
        examrecordlist=dao.findexamrecordbyrecordid(examrecord_id);
System.out.println("题目表"+examquestionlist);
        System.out.println("记录表"+examrecordlist);
        request.setAttribute("user_id", user_id);
        request.setAttribute("examrecordlist", examrecordlist);
        request.setAttribute("exampaper_id", exampaper_id);
        request.setAttribute("examquestionlist", examquestionlist);

        request.getRequestDispatcher("/user/checkmyexampaper.jsp").forward(request, response);

    }

}
